package net.hctek.identity.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import net.hctek.identity.bean.User;
import net.hctek.identity.service.IdentityServiceI;
import net.hctek.identity.service.impl.IdentityService;
import net.hctek.identity.util.Contant;
import net.hctek.identity.util.MD5;
import net.hctek.identity.util.ResponseJsonUtils;

public class LoginAction extends ActionSupport {


	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IdentityServiceI identityServiceI;
	
	private String username;
	
	private String password;
	
    private String code;
    
	@Override
	public String execute(){
		
		try {
			
			 HttpServletResponse response = ServletActionContext.getResponse();
			 Map<String,String> data=new HashMap<String,String>();
			 User user = new User();
	         String vcode = (String)ServletActionContext.getRequest().getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
	         System.out.println("vcode:"+vcode+"code:"+code);
	         if(!vcode.equals(code)){
	        	 data.put("message", "验证码错误");
	         }else{
	        	 User u = identityServiceI.getUserById(username);
	        	 
	        	 if(u == null){
	        		 data.put("message","账号错误");
	        	 }else{
	        		 String passwordx = MD5.EncoderByMd5(password);
	        		 if(!u.getPassWord().equals(passwordx)){
	        			 data.put("message","密码错误");
	        		 }else{
	        			 
	 					 ServletActionContext.getRequest().getSession().setAttribute(Contant.SESSION_USER, u);
	 					 data.put("message","欢迎"+username+"登陆");
	 					 System.out.println("登陆成功");
	        		 }
	        	 }
	         }
	        ResponseJsonUtils.json(response, data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return NONE;
	}


	

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}




	public String getUsername() {
		return username;
	}




	public void setUsername(String username) {
		this.username = username;
	}




	public String getPassword() {
		return password;
	}




	public void setPassword(String password) {
		this.password = password;
	}

	
	
	

}
